<template>
  <div>
    <div>
      <div>
        <el-input
          style="width: 200px; margin-right: 20px"
          size="mini"
          placeholder="服务名称"
          v-model="searchServer.name"
          clearable
          @keydown.enter.native="
            searchServer.currentPage = 1
            searchServer.size = 10
            initServer()
          "
        ></el-input>
        <el-button
          icon="el-icon-delete"
          type="warning"
          size="mini"
          @click="
            searchServer.name = ''
            searchServer.type = ''
          "
        >
          重置
        </el-button>
        <el-button
          type="success"
          size="mini"
          icon="el-icon-search"
          @click="
            searchServer.currentPage = 1
            searchServer.size = 10
            initServer()
          "
        >
          搜索
        </el-button>
        <el-button type="primary" size="mini" icon="el-icon-plus" @click="showAddServerView">添加服务</el-button>
      </div>
    </div>

    <div style="margin-top: 10px">
      <el-table
        size="mini"
        :data="serverData"
        border
        stripe
        :row-class-name="tableRowClassName"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column label="服务名称" align="center" prop="name"></el-table-column>
        <el-table-column label="服务key" align="center">
          <template slot-scope="scope">
            <el-tooltip v-if="scope.row.serverKey" class="item" effect="dark" placement="right-end">
              <span v-html="scope.row.serverKey.replace(/\n/gm, '<br>')" slot="content">{{ scope.row.serverKey }}</span>
              <!--<span>{{scope.row.context | ellipsis}}</span>-->
              <el-tag type="info">鼠标移动到上面查看</el-tag>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="200" align="center" prop="createTime" sortable></el-table-column>
        <el-table-column label="更新时间" width="200" align="center" prop="updateTime" sortable></el-table-column>
        <el-table-column label="操作" fixed="right" width="100">
          <template slot-scope="scope">
            <el-button size="mini" style="padding: 3px" type="primary" @click="showUpdateServerView(scope.row)">
              编辑
            </el-button>
            <el-button size="mini" style="padding: 3px" type="danger" @click="deleteServer(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div style="display: flex; justify-content: flex-end; margin-top: 5px">
      <el-pagination
        background
        layout="sizes, prev, pager, next, jumper, ->, total, slot"
        :current-page="searchServer.currentPage"
        :page-size="searchServer.size"
        :total="total"
        @current-change="currentChange"
        @size-change="sizeChange"
      ></el-pagination>
    </div>

    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      @close="closeDialog()"
      :visible.sync="dialogVisible"
      width="400px"
    >
      <el-form ref="serverForm" :model="server" :rules="rules">
        <table>
          <tr>
            <el-form-item label="服务名称" label-width="100px" prop="name">
              <el-input size="mini" style="width: 200px" v-model="server.name" placeholder="请输入服务名称"></el-input>
            </el-form-item>
          </tr>
        </table>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doServer">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { serverPageData, addServer, updateServer, deleteServer } from '@/api/alarm/server'

export default {
  name: 'Server',
  data() {
    return {
      searchServer: {
        name: null,
        currentPage: 1,
        size: 10
      },
      server: {
        id: null,
        name: ''
      },
      serverData: [],
      loading: false,
      total: 0,
      dialogVisible: false,
      title: '',
      rules: {
        name: [{ required: true, message: '请输入服务名称', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  mounted() {
    this.initServer()
  },
  methods: {
    initServer() {
      this.loading = true
      serverPageData(this.searchServer).then((resp) => {
        this.loading = false
        if (resp) {
          this.serverData = resp.data
          this.total = resp.total
        }
      })
    },
    currentChange(page) {
      this.searchServer.currentPage = page
      this.initServer()
    },
    sizeChange(size) {
      this.searchServer.size = size
      this.initServer()
    },
    deleteServer(data) {
      this.$confirm('此操作将永久删除[' + data.name + ']服务信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteServer(data.id).then((resp) => {
            if (resp) {
              this.initServer()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    showAddServerView() {
      this.server = {
        id: null,
        name: ''
      }
      this.dialogVisible = true
      this.title = '添加服务信息'
    },
    showUpdateServerView(data) {
      // 拷贝
      this.server = Object.assign({}, data)
      this.dialogVisible = true
      this.title = '编辑服务信息'
    },
    closeDialog() {
      this.$refs['serverForm'].resetFields()
    },
    doServer() {
      if (this.server.id) {
        // 编辑
        this.$refs['serverForm'].validate((valid) => {
          if (valid) {
            updateServer(this.server).then((resp) => {
              if (resp) {
                this.dialogVisible = false
                this.initServer()
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      } else {
        // 添加
        this.$refs['serverForm'].validate((valid) => {
          if (valid) {
            addServer(this.server).then((resp) => {
              if (resp) {
                this.dialogVisible = false
                this.initServer()
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
}
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

label {
  display: inline-block;
  width: 100px;
  text-align: right;
}
</style>
